#/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Created on 2011-7-27

主要用于从网站上爬取信息后，抽取页面信息；

@author: zhongfeng
'''

from pageparser import *

class GomeAllSortParser(RootCatagoryPageParser):
    '''
            从http://www.gome.com.cn/allSort.html获取所有的分类信息，
            组合成ObuyUrlSummary
    '''   
    mainHost = r'http://www.gome.com.cn' 
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(GomeAllSortParser, self).__init__(dataStr, rootUrlSummary, include, exclude) 
                             
    def getBaseSort3UrlSums(self):       
        finalUrlList = []
        allSort = self.soup.find(attrs = {'id':'allsort'})
        for t in allSort.findAll(name = 'div',attrs = {'class':'m'}):#一级
            name,url = ParserUtils.parserTag_A(t.find(attrs = {'class':'mt'}).h2.a)
            url = ''.join((self.mainHost,url))
            sort_1_urlsum = self.buildSort_N(url, name, self.rootUrlSummary, isCrawle=False)
            sort_2 = t.find(attrs = {'class':'mc'})
            for tt in sort_2(name='dl'):#二级分类
                if tt.dt.a is not None:
                    name, url = ParserUtils.parserTag_A(tt.a)
                    sort_2_urlsum = self.buildSort_N(url, name, sort_1_urlsum, isCrawle=False)
                    sort_2_urlsum.catagoryLevel = 3
                    finalUrlList.append(sort_2_urlsum)
                    continue
                name = tt.dt.getText().strip()
                url = '/'.join((self.mainHost,name))
                sort_2_urlsum = self.buildSort_N(url, name, sort_1_urlsum, isCrawle=False)
                for ttt in tt.dd.findAll(name = 'em'):#三级分类
                    name, url = ParserUtils.parserTag_A(ttt.a)
                    url = ''.join((self.mainHost,url))
                    sort_3_urlsum = self.buildSort_N(url, name, sort_2_urlsum)
                    finalUrlList.append(sort_3_urlsum)
        return finalUrlList  
        
class GomeSort3PageParser(Sort3PageParser):
    '''
          三级页面解析类
    '''    
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(GomeSort3PageParser, self).__init__(dataStr, rootUrlSummary, include, exclude)
    
    def nextPageUrlPattern(self):
        urlSegs = self.rootUrlSummary.url.rsplit('.', 1)
        pageSeg = '-0-0-0-0-0-0-0-1-1-{}'
        return '%s%s.%s' % (urlSegs[0], pageSeg, urlSegs[1])
    
    def getTotal(self):
        pageSeg = self.soup.find(name='div',attrs={'id':'toolbar'}).find(attrs={'class':'thispage'}).getText()
        totalPage = int(pageSeg.split('/')[-1])
        return totalPage
    
    def parserPageInfos(self):
        plist = self.soup.find(name='div', attrs={'id':'plist'}).find(name='ul')
        resultList = []       
        for li in plist(name='li'):
            pName,url = ParserUtils.parserTag_A(li.find(name='div', attrs={'class':'p-name'}).a)
            pid = url.rsplit('/',1)[-1].split('.')[0]
            url = ''.join((r'http://www.gome.com.cn',url))
            price = ParserUtils.getPrice(li.find(name='div', attrs={'class':'p-price'}).getText())
            prodDetail = ProductDetails(productId=pid, privPrice = price,name=pName, adWords='')
            resultList.append(prodDetail)
        return resultList

class GomeSort4PageParser(GomeSort3PageParser):
    '''
          分类四级页面为列表页面，只抽取Product信息
    '''
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(GomeSort4PageParser, self).__init__(dataStr, rootUrlSummary, include, exclude)
        
    def parserSubUrlSums(self):
        pass
            
''' test '''
import os
curModDir = os.path.dirname(os.path.abspath(__file__))
testFilePath = os.path.join(curModDir,'test_resources')
def testAllSortPage():
    fileName = os.path.join(testFilePath,'gomeAllsort.html')
    with open(fileName, 'r') as fInput:
        content = fInput.read()

    rootUrlSum = ObuyUrlSummary(url=r'http://www.gome.com.cn/allSort.html', name='gome')
    firstPage = GomeAllSortParser(content, rootUrlSum)
    for sort_3 in firstPage.getBaseSort3UrlSums():       
        for index, urlsum in enumerate(sort_3.parentPath):
            pass
        print sort_3.name,sort_3.url ,sort_3.catagoryLevel 

def testSort3Page():    
    fileName = os.path.join(testFilePath,'10000000-10000012-10000070.html')
    with open(fileName, 'r') as fInput:
        content = fInput.read()
    sort_3_urlsum = ObuyUrlSummary(url=r'http://www.gome.com.cn/products/10000000-10000012-10000070.html',
                                    parentPath=[('test')], catagoryLevel=3)
    sort3Page = GomeSort3PageParser(content, sort_3_urlsum)
    for sort_4 in sort3Page.getSort4PageUrlSums():
        print sort_4.url

def testSort3Details():
    fileName = os.path.join(testFilePath,'10000000-10000012-10000070.html')
    with open(fileName, 'r') as fInput:
        content = fInput.read()
    sort_3_urlsum = ObuyUrlSummary(url=r'http://www.gome.com.cn/products/10000000-10000012-10000070.html', 
                                   parentPath=[('test')], catagoryLevel=3)
    sort3Page = GomeSort3PageParser(content, sort_3_urlsum)  
    for product in sort3Page.parserPageInfos():
        print product.logstr()
    
if __name__ == '__main__':
    testAllSortPage()
    testSort3Page()
    testSort3Details()

    
    
